gl path "~/Dropbox/voter_id/Replication"

cap log close
log using "$path/table1.log", replace

use id dem rep hispanic black white othernw ///
	birth_year voted no_dmv_match new_reg using "$path/nc_dataset.dta" if new_reg == 0, clear
gen unaffil = dem == 0 & rep == 0 
compress

egen non_miss = rownonmiss(hispanic black white othernw birth_year voted)
gen in_sample = non_miss == 6
drop non_miss

sum voted if in_sample
local m1 = r(mean)
local sd1 = r(sd)
local min1 = r(min)
local max1 = r(max)
local n1 = r(N)

sum hispanic if in_sample
local m2 = r(mean)
local sd2 = r(sd)
local min2 = r(min)
local max2 = r(max)

sum black if in_sample
local m3 = r(mean)
local sd3 = r(sd)
local min3 = r(min)
local max3 = r(max)

sum white if in_sample
local m4 = r(mean)
local sd4 = r(sd)
local min4 = r(min)
local max4 = r(max)

sum othernw if in_sample
local m5 = r(mean)
local sd5 = r(sd)
local min5 = r(min)
local max5 = r(max)

sum birth_year if in_sample
local m6 = r(mean)
local sd6 = r(sd)
local min6 = r(min)
local max6 = r(max)

sum dem if in_sample
local m13 = r(mean)
local sd13 = r(sd)
local min13 = r(min)
local max13 = r(max)

sum rep if in_sample
local m14 = r(mean)
local sd14 = r(sd)
local min14 = r(min)
local max14 = r(max)

sum unaffil if in_sample
local m15 = r(mean)
local sd15 = r(sd)
local min15 = r(min)
local max15 = r(max)

unique id if in_sample
local n_voters1 = r(unique)

egen non_miss = rownonmiss(hispanic black white othernw birth_year voted) if no_dmv_match
replace in_sample = non_miss == 6
drop non_miss

sum voted if in_sample
local m7 = r(mean)
local sd7 = r(sd)
local min7 = r(min)
local max7 = r(max)
local n7 = r(N)

sum hispanic if in_sample
local m8 = r(mean)
local sd8 = r(sd)
local min8 = r(min)
local max8 = r(max)

sum black if in_sample
local m9 = r(mean)
local sd9 = r(sd)
local min9 = r(min)
local max9 = r(max)

sum white if in_sample
local m10 = r(mean)
local sd10 = r(sd)
local min10 = r(min)
local max10 = r(max)

sum othernw if in_sample
local m11 = r(mean)
local sd11 = r(sd)
local min11 = r(min)
local max11 = r(max)

sum dem if in_sample
local m16 = r(mean)
local sd16 = r(sd)
local min16 = r(min)
local max16 = r(max)

sum rep if in_sample
local m17 = r(mean)
local sd17 = r(sd)
local min17 = r(min)
local max17 = r(max)

sum unaffil if in_sample
local m18 = r(mean)
local sd18 = r(sd)
local min18 = r(min)
local max18 = r(max)

sum birth_year if in_sample
local m12 = r(mean)
local sd12 = r(sd)
local min12 = r(min)
local max12 = r(max)

unique id if in_sample
local n_voters2 = r(unique)

log close

quietly {
	cap log close
	set linesize 255
	log using "$path/table1.tex", text replace
	
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\small"
	noisily dis "\caption{\textbf{Descriptive Statistics, Individual Level, 2008--2018.}"
	noisily dis "\label{tab:descriptive}}"
	noisily dis "\begin{tabular}{lcccc}"
	noisily dis "\toprule \toprule"
	
	noisily dis " & Mean & Deviation & Minimum & Maximum \\"
	noisily dis " & (1) & (2) & (3) & (4)  \\[0mm]"
	noisily dis "\midrule"
	noisily dis " & \multicolumn{4}{c}{A. Full Voter File (\# Voters = " %8.0fc `n_voters1' ")} \\\cline{2-5}\\[-2mm]"
	noisily dis " Voted & " %4.3f `m1'  " & " %4.3f `sd1' " & " %4.0f `min1' " & " %4.0f `max1' " \\"
	noisily dis " Hispanic & " %4.3f `m2' " & " %4.3f `sd2' " & " %4.0f `min2' " & " %4.0f `max2'  " \\"
	noisily dis " Black & " %4.3f `m3' " & " %4.3f `sd3' " & " %4.0f `min3' " & " %4.0f `max3'  " \\"
	noisily dis " White & " %4.3f `m4' " & " %4.3f `sd4' " & " %4.0f `min4' " & " %4.0f `max4'  " \\"
	noisily dis " Other Non-White & " %4.3f `m5' " & " %4.3f `sd5' " & " %4.0f `min5' " & " %4.0f `max5'  " \\"
	noisily dis " Democrat & " %4.3f `m13' " & " %4.3f `sd13' " & " %4.0f `min13' " & " %4.0f `max13'  " \\"
	noisily dis " Republican & " %4.3f `m14' " & " %4.3f `sd14' " & " %4.0f `min14' " & " %4.0f `max14'  " \\"
	noisily dis " Unaffiliated & " %4.3f `m15' " & " %4.3f `sd15' " & " %4.0f `min15' " & " %4.0f `max15'  " \\"
	noisily dis " Birth Year & " %4.0f `m6' " & " %4.0f `sd6' " & " %4.0f `min6' " & " %4.0f `max6'  " \\[1mm]\cline{2-5}\\[-4mm]"

	noisily dis " & \multicolumn{4}{c}{B. No DMV Match (\# Voters = " %8.0fc `n_voters2' ")} \\\cline{2-5}\\[-2mm]"
	noisily dis " Voted & " %4.3f `m7'  " & " %4.3f `sd7' " & " %4.0f `min7' " & " %4.0f `max7' " \\"
	noisily dis " Hispanic & " %4.3f `m8' " & " %4.3f `sd8' " & " %4.0f `min8' " & " %4.0f `max8'  " \\"
	noisily dis " Black & " %4.3f `m9' " & " %4.3f `sd9' " & " %4.0f `min9' " & " %4.0f `max9'  " \\"
	noisily dis " White & " %4.3f `m10' " & " %4.3f `sd10' " & " %4.0f `min10' " & " %4.0f `max10'  " \\"
	noisily dis " Other Non-White & " %4.3f `m11' " & " %4.3f `sd11' " & " %4.0f `min11' " & " %4.0f `max11'  " \\"
	noisily dis " Democrat & " %4.3f `m16' " & " %4.3f `sd16' " & " %4.0f `min16' " & " %4.0f `max16'  " \\"
	noisily dis " Republican & " %4.3f `m17' " & " %4.3f `sd17' " & " %4.0f `min17' " & " %4.0f `max17'  " \\"
	noisily dis " Unaffiliated & " %4.3f `m18' " & " %4.3f `sd18' " & " %4.0f `min18' " & " %4.0f `max18'  " \\"
	noisily dis " Birth Year & " %4.0f `m12' " & " %4.0f `sd12' " & " %4.0f `min12' " & " %4.0f `max12'  " \\ "

	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{5}{p{0.6\textwidth}}{\footnotesize  The unit for voted an individual-year.  Panel A presents descriptive statistics "
	noisily dis "for the full file of North Carolina voters registered as of the 2014 general election.  In this panel, every observation is a voter within "
	noisily dis "an election period.  Panel B presents descriptive statistics for the individuals in the voter file who do not match "
	noisily dis "to a DMV identification record.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	
	log off
	
}


